<!DOCTYPE html>
<html style="width:100%;height:100%;">
<head lang="en">
    <meta charset="UTF-8">
    <title>导航菜单列表</title>
    <link type="text/css" rel="stylesheet" href="/resources/easyui/themes/default/easyui.css">
    <link type="text/css" rel="stylesheet" href="/resources/easyui/themes/color.css">
    <link type="text/css" rel="stylesheet" href="/resources/easyui/themes/icon.css">
    <link type="text/css" rel="stylesheet" href="/resources/css/yidong.css">
    <script type="text/javascript" src="/resources/easyui/jquery.min.js"></script>
    <script type="text/javascript" src="/resources/easyui/jquery.easyui.min.js"></script>
    <script type="text/javascript" src="/resources/easyui/locale/easyui-lang-zh_CN.js"></script>
    <script type="text/javascript" src="/resources/easyui/plugin/jquery.cookie.js"></script>
    <script type="text/javascript" src="/resources/js/Util.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){
            loadMenuList();
        });
        function loadMenuList(successFun){
            getMenu(function(data){
                $('#menuList').treegrid('loadData',data);
                if(successFun) successFun();
            });
        }
        function getMenu(successFun){
            restClient('/rest/menu/list', successFun);
        }
        function loadMenuCombo(defaultValue){
            var values=[];
            values.push({id:0, text:'根菜单'});
            getMenu(function(data){
                var parentId = $('#parentId');
                if(data) {
                    for(var index in data) {
                        values.push(data[index]);
                    }
                    parentId.combobox('loadData', values);
                    if(defaultValue) {
                        parentId.combobox('setValue', defaultValue);
                    } else {
                        parentId.combobox('setValue', 0);
                    }
                }
            });
        }
    </script>
</head>
<body onload="isLogon();" style="width:100%;height:100%;margin: 0;">
<table id="menuList" class="easyui-treegrid" style="width:100%;height:100%;"
       toolbar="#toolbar" data-options="idField:'id',treeField:'text'"
       rownumbers="true" fitColumns="true" singleSelect="true">
    <thead>
        <tr>
            <th data-options="field:'text',width:100">菜单名</th>
            <th data-options="field:'url',width:100">链接</th>
            <th data-options="field:'sort',width:50">顺序</th>
            <th data-options="field:'status',width:50,formatter:formatStatus">状态</th>
        </tr>
    </thead>
</table>
<div id="toolbar">
    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="newOne()">新建</a>
    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-edit" plain="true" onclick="editOne()">编辑</a>
    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="destroyOne()">删除</a>
</div>
<div id="dlg" class="easyui-dialog" style="width:400px;height:auto;padding:-20px"
     closed="true" buttons="#dlg-buttons">
    <form id="fm" class="easyui-form" method="post">
        <div class="fitem">
            <label>父菜单：</label>
            <input id="parentId" name="parentId" class="easyui-combobox easyui-validatebox"
                   data-options="valueField:'id', textField:'text', editable:false" required="true">
        </div>
        <div class="fitem">
            <label>菜单名：</label>
            <input id="name" name="name" class="easyui-textbox easyui-validatebox"
                   required="true" data-options="missingMessage:'该输入项为必输项'">
        </div>
        <div class="fitem">
            <label>链接：</label>
            <input id="url" name="url" class="easyui-textbox">
        </div>
        <div class="fitem">
            <label>顺序：</label>
            <input id="sort" name="sort" class="easyui-textbox easyui-validatebox"
                   required="true" data-options="missingMessage:'该输入项为必输项'">
        </div>
        <div class="fitem">
            <label>状态：</label>
            <select id="status" name="status" class="easyui-combobox easyui-validatebox" data-options="editable:false" required="true">
                <option value="0">启用</option>
                <option value="-1">禁用</option>
                <option value="-2">删除</option>
            </select>
        </div>
    </form>
</div>
<div id="dlg-buttons">
    <a href="javascript:void(0)" class="easyui-linkbutton c6" iconCls="icon-ok" onclick="saveOne()" style="width:90px">保存</a>
    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$('#dlg').dialog('close')" style="width:90px">取消</a>
</div>
<script type="text/javascript">
    var url;
    var successMsg;
    function newOne(){
        $('#dlg').dialog('open').dialog('setTitle','新建');
        $('#fm').form('clear');
        loadMenuCombo();
        $('#status').combobox('setValue', 0);
        url = '/rest/menu/create';
        successMsg = "添加成功";
    }
    function editOne(){
        var row = $('#menuList').datagrid('getSelected');
        if (row) {
            $('#dlg').dialog('open').dialog('setTitle', '编辑');
            loadMenuCombo(row.parentId);
            $('#name').textbox('setValue', row.text);
            $('#url').textbox('setValue', row.url);
            $('#sort').textbox('setValue', row.sort);
            $('#status').combobox('setValue', row.status);
            url = '/rest/menu/update/'+row.id;
            successMsg = "编辑成功";
        }
    }
    function saveOne(){
        restClient(url, function (result) {
            if (result.success) {
                $('#dlg').dialog('close');
                loadMenuList(function () {
                    $.messager.show({
                        title: '温馨提醒',
                        msg: successMsg
                    });
                });
            } else {
                $.messager.show({
                    title: '温馨提醒',
                    msg: result.msg ? result.msg : result.message
                });
            }
        }, $('#fm').serialize());
    }
    function destroyOne(){
        var row = $('#menuList').datagrid('getSelected');
        if (row){
            var msg = '确认要删除【'+row.text+'】么?';
            if(row.parentId === 0) {
                msg = '确认要删除【'+row.text+'】及它的子菜单么?';
            }
            $.messager.confirm('确认',msg,function(r){
                if (r){
                    restClient('/rest/menu/delete/'+row.id, function(result){
                        if (result.success){
                            loadMenuList(function(){
                                $.messager.show({
                                    title: '温馨提醒',
                                    msg: '删除成功'
                                });
                            });
                        } else {
                            $.messager.show({
                                title: '温馨提醒',
                                msg: result.msg ? result.msg : result.message
                            });
                        }
                    });
                }
            });
        }
    }
</script>
</body>
</html>